package cn.wh.pet.controller;

import cn.wh.basic.exception.BusinessException;
import cn.wh.basic.util.AjaxResult;
import cn.wh.basic.util.LoginContext;
import cn.wh.basic.util.PageList;
import cn.wh.org.domain.Employee;
import cn.wh.org.service.IEmployeeService;
import cn.wh.pet.domain.SearchMasterMsg;
import cn.wh.pet.dto.HandleMsgDto;
import cn.wh.pet.query.SearchMasterMsgQuery;
import cn.wh.pet.service.ISearchMasterMsgService;
import cn.wh.user.domain.Logininfo;
import cn.wh.user.domain.User;
import cn.wh.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IEmployeeService iEmployeeService;

    @Autowired
    private IUserService iUserService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.my();
        }catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.removeById(id);
            return AjaxResult.my();
        }catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryPage(query);
    }


    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody  SearchMasterMsg smg , HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContext.getCurrentLogininfo(request);
            searchMasterMsgService.publish(smg , logininfo);
            return AjaxResult.my();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！"+e.getMessage());
        }

    }

    //1.个人中心-我的消息
    @PostMapping("/mymsg")
    public PageList<SearchMasterMsg> mymsg(@RequestBody SearchMasterMsgQuery query , HttpServletRequest request){
        Logininfo logininfo = LoginContext.getCurrentLogininfo(request);
        User user = iUserService.loadByLoginInfo(logininfo.getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.queryPage(query);
    }

    //2.待审核消息
    @PostMapping("/reviewed")
    public PageList<SearchMasterMsg> reviewed(@RequestBody SearchMasterMsgQuery query){
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }

    //3.寻主池消息
    @PostMapping("/master")
    public PageList<SearchMasterMsg> master(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }

    //4.待处理消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> Pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getCurrentLogininfo(request);
        Employee employee = iEmployeeService.loadByLogininfoId(logininfo.getId());
        Long shop_id = employee.getShop_id();
        query.setShopId(shop_id);
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }
    //5.已处理消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query){
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }
//-1驳回  0待审核  1审核通过  2待处理

    /**
     * 接单
     * @param dto
     * @return
     */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandleMsgDto dto) {
        try {
            searchMasterMsgService.accept(dto);
            return AjaxResult.my();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }

    }

    /**
     * 拒单
     * @param id
     * @return
     */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.my();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }

    }



}
